<?php

namespace App\Service;

use App\Exception\BusinessException;
use App\Logic\SmsLogic;
use App\Mapper\UserMapper;
use App\Model\SmsModel;
use App\Model\UserModel;
use App\Utils\SmsUtil;
use Hyperf\Di\Annotation\Inject;

class UserService
{
    #[Inject]
    protected SmsLogic $smsLogic;

    /**
     * 使用账号密码注册
     * @param string $account
     * @param string $password
     * @return UserModel|\Hyperf\Database\Model\Model
     */
    public function registerByAccount(string $account, string $password)
    {
        UserMapper::existsOrFailByAccount($account);

        //把md5放在mapper外面， 因为加密算法属于逻辑代码； mapper只负责与数据模型交互; 如有必要也可以使用其他算法替换md5
        return UserMapper::createUserByAccount($account, md5($password));
    }


    /**
     * 使用手机号注册
     * @param string $mobile
     * @param $code
     * @param string $event
     * @return UserModel|\Hyperf\Database\Model\Model
     */
    public function registerByMobile(string $mobile, $code)
    {
        $this->smsLogic->checkSms($mobile, $code, 'register');

        return UserMapper::createUserByMobile($mobile);
    }


    /**
     * 发送短信
     * @param string $mobile
     * @param string $event
     * @return int
     */
    public function sendSms(string $mobile, $event =  'register')
    {
       return $this->smsLogic->sendSms($mobile, $event);
    }
}